package com.devilvan.betrice.lol.controller;

import com.devilvan.betrice.common.BetriceResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;

/**
 * @author Evad.Wu
 * @Description 文件上传 控制器
 * @date 2022-10-04
 */
@RestController
@RequestMapping(value = "fileController")
@Tag(name = "文件上传 控制器")
public class FileController {
    /**
     * 上传文件
     * @param request 请求体
     * @param response 响应体
     * @return 形式返回7
     *
     */
    @Operation(summary = "上传文件")
    @PostMapping(value = "doFileUpload")
    public BetriceResultVo<Object> doFileUpload(HttpServletRequest request, HttpServletResponse response) {
        BufferedOutputStream bufferedOutputStream = null;
        BufferedInputStream bis = null;
        try {
            String fileName = request.getParameter("fileName");
            System.out.println(fileName);
            Part file = request.getPart("file");
            InputStream inputStream = file.getInputStream();
            bis = new BufferedInputStream(inputStream);
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("D:\\Mine\\杂物\\file.zip"));
            byte[] bs = new byte[1024 * 1024];
            int len;
            while ((len = bis.read(bs)) != -1) {
                bufferedOutputStream.write(bs, 0, len);
            }
        } catch (IOException | ServletException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedOutputStream != null) {
                    bufferedOutputStream.close();
                }
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
